package com.wdl.modules.myhouse.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wdl.modules.myhouse.dao.SysUserDao;
import com.wdl.modules.myhouse.dao.UserDao;
import com.wdl.modules.myhouse.entity.UserEntity;
import com.wdl.modules.myhouse.entity.sys.SysUserEntity;
import com.wdl.modules.myhouse.entity.sys.SysUserRoleEntity;
import com.wdl.modules.myhouse.service.SysUserRoleService;
import com.wdl.modules.myhouse.service.SysUserService;
import com.wdl.modules.myhouse.service.UserService;
import com.wdl.modules.myhouse.vo.PageVO;
import com.wdl.modules.myhouse.vo.SysUserAddVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Map;

/**
 * @author 昏昏的君
 */
@Service
public class SysUserServiceImpl extends ServiceImpl<SysUserDao, SysUserEntity> implements SysUserService {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private UserService userService;

    @Autowired
    private SysUserRoleService userRoleService;


    @Override
    public Map<Integer, Object> getAllUserDetails() {
        return userDao.selectAllUser();
    }

    @Override
    public Map<Integer, Object> getUserInfoByPage(PageVO page) {
        Integer currentPage = page.getCurrentPage();
        Integer pageSize = page.getPageSize();
        Integer start = (currentPage - 1) * pageSize;
        return sysUserDao.getUserInfoByPage(start, pageSize);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveUserWithDetail(SysUserAddVO addVO) {

        UserEntity userDesc = new UserEntity();
        SysUserEntity userEntity = new SysUserEntity();
        //获取系统用户信息
        userEntity.setUsername(addVO.getUsername());
        userEntity.setPassword(new BCryptPasswordEncoder().encode(addVO.getPassword()));

        //获取用户描述信息
        userDesc.setAvatarUrl(addVO.getAvatarUrl());
        userDesc.setMail(addVO.getMail());

        //保存用户基本信息
        sysUserDao.insert(userEntity);
        //保存 用户描述
        if (userEntity.getId() == null) {
            throw new RuntimeException("保存系统用户信息失败");
        }
        userDesc.setUserId(userEntity.getId());
        userService.save(userDesc);

        //保存用户角色信息
        Integer[] roles = addVO.getRoleId();
        ArrayList<SysUserRoleEntity> userRoles = new ArrayList<>();
        SysUserRoleEntity sysUserRoleEntity;
        if (roles != null && roles.length > 0) {
            for (Integer roleId : roles) {
                sysUserRoleEntity = new SysUserRoleEntity();
                sysUserRoleEntity.setRid(roleId);
                sysUserRoleEntity.setUid(userEntity.getId());
                userRoles.add(sysUserRoleEntity);
            }
        } else {
            //默认角色为普通用户
            sysUserRoleEntity = new SysUserRoleEntity();
            sysUserRoleEntity.setRid(1);
            sysUserRoleEntity.setUid(userEntity.getId());
            userRoles.add(sysUserRoleEntity);
        }
        userRoleService.saveBatch(userRoles);
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateDetailById(Integer id, SysUserAddVO vo) {


        UserEntity userDesc = new UserEntity();
        SysUserEntity userEntity = new SysUserEntity();
        //获取系统用户信息
        userEntity.setUsername(vo.getUsername());
        userEntity.setPassword(new BCryptPasswordEncoder().encode(vo.getPassword()));

        //获取用户描述信息
        userDesc.setAvatarUrl(vo.getAvatarUrl());
        userDesc.setMail(vo.getMail());

        userEntity.setId(id);
        sysUserDao.updateById(userEntity);
        //保存 用户描述

        userDesc.setUserId(userEntity.getId());
        userService.updateById(userDesc);

        //删除用户之前的角色信息
        QueryWrapper<SysUserRoleEntity> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(SysUserRoleEntity::getUid, id);
        userRoleService.remove(wrapper);

        //保存用户角色信息
        Integer[] roles = vo.getRoleId();
        ArrayList<SysUserRoleEntity> userRoles = new ArrayList<>();
        for (Integer roleId : roles) {
            SysUserRoleEntity sysUserRoleEntity = new SysUserRoleEntity();
            sysUserRoleEntity.setRid(roleId);
            sysUserRoleEntity.setUid(userEntity.getId());
            userRoles.add(sysUserRoleEntity);
        }
        userRoleService.saveBatch(userRoles);


    }
}
